<?php
namespace app\user\controller;
use \think\Controller;
use \think\Request;
use \think\Session;
use app\user\traits\models\UserInfoCheck;
use app\common\traits\controller\TraitController;
use Carbon\Carbon;
/**
 * 前台房车分享
 */
class SetCaravan extends Controller
{  
    use UserInfoCheck;
    use TraitController;
    //前置操作
    protected $beforeActionList = [
        'checkLogin',
        'checkCaravanStatus'
    ];
    /**
     * [checkLogin 判断是否登陆]
     * @return [type] [description]
     */
    protected function checkLogin()
    {
        if (!model('Users')->checkLogin()) {
            Session::set('redirect_url', Request::instance()->url(true));
            $this->redirect('@user/login');
            exit;
        }
    }
    /**
     * [checkCaravanStatus 房车是否审核通过]
     * @return [type] [description]
     */
    protected function checkCaravanStatus()
    {
        //1=审核通过
        $status = model('CaravanInfos')->where('user_id',session('user.id'))->value('status');
        if ($status<>1) {
            $this->error('房车未审核通过','/caravaninfo/index');
            
        }
    }
    public function getShareCaravan()
    {
        //$a = model('Calendar')->where('caravan_id',2)->find();halt($a->user_id);
        // $caravan_calendar = model('user/CarCalendar');
        // $today     = Carbon::now()->toDateString();
        // $yestoday     = Carbon::now()->addDay(-1)->toDateString();
        // $object = $caravan_calendar->where('e',$yestoday)->select();
        // halt($object);

        $user_id = session('user.id');//halt($user_id);
        $caravan_id = model('Caravans')->where('user_id',$user_id)->value('id');
        $this->assign('caravan_id',$caravan_id);//halt($caravan_id);
        $share = model('Calendar')->getCaravanStatus($caravan_id);//halt($share);
        $this->assign('share',$share['shares']);//获取日历表房车的分享状态
        //halt($share['shares']);
        $this->assign('rent',$share['rents']);//获取日历表房车的出租状态
        $this->assign('disables',$share['disables']);//获取日历表房车的订单状态

        $renta = model('Calendar')->where('caravan_id',$caravan_id)->where('rents','1')->column('single_day_price', 'datetimes');
        $this->assign('renta',json_encode($renta));//

        $camp_id = model('Users')->where('id',$user_id)->value('camp_id');//halt($camp_id);
        $travel_money_base = model('Camps')->where('id',$camp_id)->value('travel_money_base');
        $this->assign('travel_money_base',$travel_money_base);//营地旅行币基数
        return view('setcaravan/caravanshare');
    }

    public function postIntergrate()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.startdate');
        $end_date = input('post.enddate');
        $intergrate = model('Calendar')->getIntergrtates($caravan_id, $start_date, $end_date);
        return $intergrate;
    }

    public function postReduceIntergrate()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.startdate');
        $end_date = input('post.enddate');
        $intergrate = model('Calendar')->getReduceIntergrtates($caravan_id, $start_date, $end_date);
        return $intergrate;
    }

    public function postCash()
    {
        $caravan_id = input('post.caravan_id');//halt($end_date);
        $cash = input('post.cash');
        $start_date = input('post.startdaterent');
        $end_date = input('post.endaterent');
        return $cash = $cash * (model('Calendar')->diffDays($start_date, $end_date, $abs = true) + 1);
    }

    public function postShareCaravan()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.startdate');
        $end_date = input('post.endate');
        $today = date('Y-m-d');
        if ($start_date == '' || $end_date == '') {
            return $this->error('请选择分享时间');
        }

        // $counts = count(model('Calendar')->where('caravan_id',$caravan_id)->where('datetimes','>',$today)->where('shares','1')->select());
        // $datetime = model('Calendar')->diffDays($start_date, $end_date, $abs = true);
        // $counts = $counts + $datetime;
        // if ($counts >= 30) {
        //     return $this->error('分享时间最长1个月,您已超出',$url = null,'',$wait = 8);
        // }//判断分享时间是否超过一个月
        $today = date("Y-m-d");
        $time = model('Calendar')->diffDays($today, $start_date, $abs = false);
        if ($time<7) {
            return $this->error('分享日期错误');
        }

        $whethershare = model('Calendar')->where('caravan_id', $caravan_id)->where('datetimes', 'between', [$start_date, $end_date])->where('shares','1')->select();
        if ($whethershare) {
            return $this->error('该时间段已被分享');
        }

        $check = model('Calendar')->checkUserCaravanDateSetShare($caravan_id, $start_date, $end_date);
        if ($check) {
            
            $count=model('Calendar')->share($caravan_id, $start_date, $end_date);//return;
            if ($count) {
                $url = Session::pull('redirect_url');
                if ($url == '/wishs/create') {
                    return $this->success('分享成功',$url);
                }
                if ($url == url("/filter/exlist",'','','www')) {
                    return $this->success('分享成功',$url);
                }
                return $this->success('分享成功','/setcaravan/sharecaravan');
            }else{
                return $this->error('分享失败');
            }
        }else{
            return $this->error('分享失败');
        }
        
        
    }

    public function postCancelShare()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.csdate');
        $end_date = input('post.cedate');
        
        $count = model('Calendar')->cancelCaravanShare($caravan_id, $start_date, $end_date);
        if ($count) {
            return $this->success('取消分享成功','/setcaravan/sharecaravan');
        }else{
                return $this->error('取消分享失败');
            }
    }

    public function getRentCaravan()
    {
        $user_id = session('user.id');
        $caravan_id = model('Caravans')->where('user_id',$user_id)->value('id');
        $this->assign('caravan_id',$caravan_id);
        $integral = model('Caravans')->where('user_id',$user_id)->value('integral');
        $this->assign('integral',$integral);
        $camp_id = model('Users')->where('id',$user_id)->value('camp_id');
        $rent_money_base = model('Camps')->where('id',$camp_id)->value('rent_money_base');
        $this->assign('rent_money_base',$rent_money_base);

        
        $rent = model('Calendar')->getCaravanStatus($caravan_id);//halt($rent);
        $this->assign('share',$rent['shares']);//获取日历表房车的分享状态
        $this->assign('rent',$rent['rents']);//获取日历表房车的出租状态
        $this->assign('disables',$rent['disables']);//获取日历表房车的订单状态

        $renta = model('Calendar')->where('caravan_id',$caravan_id)->where('rents','1')->column('single_day_price', 'datetimes');
        $this->assign('renta',json_encode($renta));//

       

        return view('setcaravan/caravanrent');
    }

    public function postRentCaravan()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.startdaterent');//halt($start_date);
        $end_date = input('post.endaterent');
        $single_day_price = input('post.single_day_price');//halt($single_day_price);

        if ($start_date == '' || $end_date == '') {
            return $this->error('请选择出租时间');
        }

        if ($single_day_price == '') {
            return $this->error('请设置出租单日价格');
        }

        $today = date("Y-m-d");
        $time = model('Calendar')->diffDays($today, $start_date, $abs = false);
        if ($time<0) {
            return $this->error('出租日期错误');
        }

        $check = model('Calendar')->checkUserCaravanDateSetRent($caravan_id, $start_date, $end_date);
        if ($check) {
            $count=model('Calendar')->rent($caravan_id, $start_date, $end_date, $single_day_price);
            if ($count) {
                return $this->success('出租成功','/setcaravan/rentcaravan');
            }else{
                return $this->error('出租失败');
            }
        }else{
            return $this->error('出租失败');
        }

    }
    //取消出租
    public function postCancelRent()
    {
        $caravan_id = input('post.caravan_id');
        $start_date = input('post.csdate');
        $end_date = input('post.cedate');
        $single_day_price = 0;

        $count = model('Calendar')->cancelCaravanRent($caravan_id, $start_date, $end_date, $single_day_price);//return;
        if ($count) {
            return $this->success('取消出租成功','/setcaravan/rentcaravan');
        }else{
                return $this->error('取消出租失败');
       	}
    }

    
}

